package com.hczt.xhminiapp.adminapi.modules.system.controller;



import com.hczt.xhminiapp.adminapi.modules.system.service.SysRoleService;
import com.hczt.xhminiapp.adminapi.modules.system.vo.AllotMenu;
import com.hczt.xhminiapp.adminapi.modules.system.vo.SysRoleQuery;
import com.hczt.xhminiapp.common.bean.RtnResult;
import com.hczt.xhminiapp.db.entity.SysPermission;
import com.hczt.xhminiapp.db.entity.SysRole;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 红创-马海强
 * @date 2019-03-13 13:40
 * @description 系统角色管理
 */
@RestController
@RequestMapping("/sys/roles")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    /**
     * 系统角色分页列表
     * @param query
     * @param pageable
     * @return
     */
    @GetMapping("/page")
    public Object page(SysRoleQuery query, @PageableDefault Pageable pageable){
        Page<SysRole> list = sysRoleService.findSysRoles(query, pageable);
        return RtnResult.success(list);
    }

    /**
     * 系统角色详情
     * @param roleId
     * @return
     */
    @GetMapping("/detail")
    public Object detail(@RequestParam String roleId){
        SysRole sysRole = sysRoleService.findSysRoleDetail(roleId);
        return RtnResult.success(sysRole);
    }

    /**
     * 系统角色添加
     * @param sysRole
     * @return
     */
    @PostMapping("/add")
    public Object add(@RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);
        return RtnResult.success();
    }

    /**
     * 系统角色修改
     * @param sysRole
     * @return
     */
    @PostMapping("/edit")
    public Object edit(@RequestBody SysRole sysRole){
        sysRoleService.update(sysRole);
        return RtnResult.success();
    }

    /**
     * 系统角色删除
     * @param sysRole
     * @return
     */
    @PostMapping("/remove")
    public Object remove(@RequestBody SysRole sysRole){
        sysRoleService.remove(sysRole.getRoleId());
        return RtnResult.success();
    }

    /**
     * 系统角色菜单
     * @param roleId
     * @return
     */
    @GetMapping("/menus")
    public Object roles(@RequestParam String roleId){
        List<SysPermission> menus = sysRoleService.findSysRoleMenus(roleId);
        return RtnResult.success(menus);
    }

    /**
     * 分配角色菜单
     * @return
     */
    @PostMapping("/allot-menu")
    public Object allotRole(@RequestBody AllotMenu allotMenu){
        sysRoleService.allotMenu(allotMenu);
        return RtnResult.success();
    }
}
